package com.example.spring.demo.controller;

import lombok.Data;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;

/**
 * @author liangchao03
 * 2019/5/20
 */
@RestController
@RequestMapping("/spring")
public class SpringController {


    @PostMapping("/validate")
    public String validateParam(@Validated ValidParam param, BindingResult bindingResult) {
        System.out.println(bindingResult.getModel());
        System.out.println(param);
        return "success";
    }
}

@Data
class ValidParam {
    @Min(value = 0, message = "Invalid Id")
    private int id;
    @NotBlank(message = "Invalid Name")
    private String name;
    @Min(value = 18, message = "Too Small")
    @Max(value = 99, message = "Too Big")
    private int age;
}
